//
//  ListFeedViewController.m
//  Doc Bao
//
//  Created by Hai Nguyen on 10/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ListFeedViewController.h"
#import "ListPostViewController.h"
#import "FeedCell.h"
#import "Feed.h"
#import "FeedExtensions.h"
#import "News.h"
#import "NewsExtensions.h"

@implementation ListFeedViewController
@synthesize feedTable;
@synthesize feedArray;
@synthesize feedCell;

- (void)layoutForTheNews:(News*) theNews{
	[self.loadingView setHidden:NO];
	if (self.feedArray == nil) {
		feedArray = [[NSMutableArray alloc] init];
	}
	[self.titleLabel setText:theNews.Name];
	[feedArray removeAllObjects];
	[feedArray setArray:[Feed getFeeds:[theNews.Id intValue]]];
	NSLog(@"Feed %@",feedArray);
	[feedTable reloadData];
	[self.loadingView setHidden:YES];
}

#pragma mark -
#pragma mark UITableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [feedArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 50.0f;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *feedCellId = @"feedCellId";
	feedCell = (FeedCell*)[feedTable dequeueReusableCellWithIdentifier:feedCellId];
	if (feedCell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"FeedCell" owner:self options:nil];
	}
	Feed *feed = [feedArray objectAtIndex:indexPath.row];
	[feedCell.nameLabel setText:feed.Name];
	return [feedCell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	Feed *feed = [feedArray objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:listPostController animated:YES];
	[listPostController layoutForTheFeed:feed];
}

#pragma mark -

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	listPostController = [[ListPostViewController alloc] init];
	[self.view addSubview:loadingView];
	[loadingView setFrame:CGRectMake(0, 44, 320, 367)];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.feedTable = nil;
	self.feedCell = nil;
}


- (void)dealloc {
	 [feedTable release];
	 [feedArray release];
	 [feedCell release];
	[listPostController release];
    [super dealloc];
}


@end
